<script setup>
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
import api from '@/apis'
import { showNotify } from 'vant'
import community from './components/community.vue'
import wallet from './components/wallet.vue'
const { t } = useI18n()
const communityComponent = ref(community)
const walletComponent = ref(wallet)

const tabs = ref([
  {
    label: '我的钱包',
    component: walletComponent
  },
  {
    label: '我的社区',
    component: communityComponent
  },
])

const selectIndex = ref(0)
const tabsChange = (tab, index) => {
  if (selectIndex.value == index) return
  selectIndex.value = index
}

// 复制内容到剪贴板
const copyContent = async (content, type) => {
  const targetUrl = window.location.href
  const urlObj = new URL(targetUrl)
  let text = urlObj.origin + "/#/index?code=" + content
  try {
    await navigator.clipboard.writeText(text)
    showNotify({ type: 'success', message: '复制成功' })
    return true
  } catch (err) {
    // 如果不支持Clipboard API，使用备用方法
    if (type === 'html') {
      // HTML内容使用execCommand无法直接复制
      throw new Error('浏览器不支持HTML复制')
    }
    return fallbackCopyText(text)
  }
}

// 备用复制方法
const fallbackCopyText = (text) => {
  return new Promise((resolve, reject) => {
    const textarea = document.createElement('textarea')
    textarea.value = text
    textarea.style.position = 'fixed'
    textarea.style.top = '0'
    textarea.style.left = '0'
    textarea.style.opacity = '0'
    document.body.appendChild(textarea)

    try {
      textarea.select()
      const success = document.execCommand('copy')
      document.body.removeChild(textarea)

      if (success) {
        showNotify({ type: 'success', message: '复制成功' })
        resolve(true)
      } else {
        reject(new Error('execCommand复制失败'))
      }
    } catch (err) {
      document.body.removeChild(textarea)
      reject(err)
    }
  })
}
</script>
<template>
  <div class="container p-30">
    <div class="relative z-1">
      <div class="flex items-center">
        <div class="flex items-center p-8px rounded-[10px] bg-[#272727]">
          <div
            :class="['text-[24px] text-[#fff] font-[400] font-pinfang px-40px py-14px rounded-[10px]', selectIndex == index ? 'bg-[#F76938]' : '']"
            v-for="(value, index) in tabs" :key="index" @click="() => tabsChange(value, index)">{{ value.label }}</div>
        </div>
      </div>
    </div>
    <div class="mt-30px">
      <component :is="tabs[selectIndex].component"></component>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.van-hairline--surround {
  padding: 6px 10px;
  border-radius: 6px;
}

.container {
  width: 100%;
  min-height: 100vh;
  background: #101010;

  // padding-top: calc(220px / 2);
  // height: 100vh;
  // display: flex;
  // flex-direction: column;
  // background-color: #f5f5f5;
  .body {
    width: 100%;
    position: relative;
  }
}
</style>
